package com.qiyeji.real_estate.controller;

import com.qiyeji.real_estate.entity.*;
import com.qiyeji.real_estate.service.*;
import com.qiyeji.real_estate.utils.ExcelUtils;
import com.qiyeji.real_estate.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@RequestMapping("/ExcellFile")
@RestController
public class ExcellFileController {

    @Autowired
    StaffService staffService;

    @Autowired
    ProjectService projectService;

    @Autowired
    CustomerService customerService;

    @Autowired
    StaffPerformanceService staffPerformanceService;

    @Autowired
    PayrollService payrollService;

    private CopyOnWriteArrayList<customer> customers;

    private CopyOnWriteArrayList<staff> staffList;

    private CopyOnWriteArrayList<project> projects;

    private CopyOnWriteArrayList<StaffPerformance> staffPerformances;

    private CopyOnWriteArrayList<payroll> payrolls;

    public CopyOnWriteArrayList<staff> getStudentList() {
        return staffList;
    }

    public void setStudentList() {
        this.staffList = staffService.getAllStaffForExcel();
    }

    public void setProjects() {
        this.projects = projectService.getAllProjectForExcel();
    }

    public CopyOnWriteArrayList<project> getProjects() {
        return projects;
    }

    public void setCustomers() {
        this.customers = customerService.getAllCustomerForExcel();
    }

    public CopyOnWriteArrayList<customer> getCustomers() {
        return customers;
    }

    public void setStaffPerformances(CopyOnWriteArrayList<StaffPerformance> staffPerformances) {
        this.staffPerformances = staffPerformances;
    }

    public CopyOnWriteArrayList<StaffPerformance> getStaffPerformances() {
        return staffPerformances;
    }

    public void setPayrolls(CopyOnWriteArrayList<payroll> payrolls) {
        this.payrolls = payrolls;
    }

    public CopyOnWriteArrayList<payroll> getPayrolls() {
        return payrolls;
    }

    @GetMapping("/GetExcelFileForStaff")
    public void getExcelFile(HttpServletResponse response) throws Exception {
        try {
            setStudentList();
            ExcelUtils.writeExcel("员工信息" ,staff.class ,response,getStudentList());
        } catch (Exception e) {
            throw new Exception("导出excel表格失败!", e);
        }
    }

    @GetMapping("/GetExcelFileForProject")
    public void getExcelFileForProject(HttpServletResponse response) throws Exception {
        try {
            setProjects();

            ExcelUtils.writeExcel("项目汇总表", project.class, response, getProjects());
        } catch (Exception e){
            throw new Exception("导出excel表格失败", e);
        }
    }

    @GetMapping("/GetExcelFileForCustomer")
    public void getExcelFileForCustomer(HttpServletResponse response) throws Exception {
        try {
            setCustomers();
            ExcelUtils.writeExcel("客户信息报表", customer.class, response, getCustomers());
        } catch (Exception e){
            throw new Exception("导出excel表格失败", e);
        }
    }

    @RequestMapping("/SetStaffPerformanceByDate")
    public Result setStaffPerformanceYear(@RequestBody Map<String, String> params) {
        setStaffPerformances(staffPerformanceService.getStaffPerformanceByDateForExcel(params.get("date")));
        System.out.println(staffPerformances.size());
        for (StaffPerformance s: staffPerformances){
            System.out.println(s);
        }
        if (staffPerformances.size() == 0)
            return Result.error("本月没有任何数据！");
        else
            return Result.success(true);
    }

    @PostMapping("/GetStaffPerformanceByDate")
    public Result getStaffPerformanceByDate(@RequestBody Map<String, String> params) {
        CopyOnWriteArrayList<StaffPerformance> res = staffPerformanceService.getStaffPerformanceByDateForExcel(params.get("date"));
        System.out.println(res.toString());
        if (res == null) {
            return Result.error("没有任何记录！");
        }
        else
            return Result.success(res);
    }

    @RequestMapping("/GetExcelFileForStaffPerformance")
    public void getExcelFileForStaffPerformance(HttpServletResponse response) throws Exception {
        try {
            ExcelUtils.writeExcel("员工绩效报表", StaffPerformance.class, response, getStaffPerformances());
        } catch (Exception e) {
            throw new Exception("导出excel表格失败", e);
        }
    }

    @RequestMapping("/SetPayrollByDate")
    public Result setPayrollByDate(@RequestBody Map<String, String> params) {
        setPayrolls(payrollService.getPayrollForExcel(params.get("date")));
        System.out.println(payrolls.size());
        for (payroll s: payrolls){
            System.out.println(s.toString());
        }
        if (payrolls.size() == 0)
            return Result.error("本月没有任何数据！");
        else
            return Result.success(true);
    }

    @RequestMapping("/GetPayrollForExcel")
    public void getPayrollForExcel(HttpServletResponse response) throws Exception {
            try {
                ExcelUtils.writeExcel("本月薪资报表", payroll.class, response, getPayrolls());
            } catch (Exception e) {
                throw new Exception("导出excel表格失败", e);
            }

    }

//    //先从前端获取studentList
//    @RequestMapping("/getStudent")
//    public void getStudent(@RequestBody CopyOnWriteArrayList<staff> staffList)  {
//        setStudentList(staffList);
//    }


//    @PostMapping("/GetExcellFileForStaff")
//    public Result getExcellFile(@RequestBody Map<String, String> params) {
//        String year = params.get("year");
//
//        return Result.success();
//    }
}
